package cn.kgc.mvc.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.lang.model.element.VariableElement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author: mosin
 * @version: v1.0  2022/7/8
 * 非法请求拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {


    // Interceptor    ->  boolean    false  拦截  true  放行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String servletPath = request.getServletPath();
        System.out.println("servletPath = " + servletPath);

        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");

        if (user == null) { // 用户没有登录  将请求重定向到登录页
            response.sendRedirect(request.getContextPath() + "/login.jsp");
            return false;
        }

        return true;
    }
}
